* Generate csv files needed for the graphs

******************************
* INCLUDE INCUMBENT CONTEST
******************************


*************************
* SCAD-NELDA master data
*************************

use "scad_nelda_master.dta", clear
desc
count

sort ccode day

* No elections
bys ccode: summ numbelec_all_contest
drop if ccode == 520
drop if ccode == 572
drop if ccode == 600
drop if ccode == 620

*distance to election
levelsof ccode, local(levels)

foreach l of local levels {
di `l'

levelsof day if numbelec_all_contest & ccode == `l', local(elecdaylist)

foreach elecday of local elecdaylist {

di `elecday'
cap drop day2elec`l'`elecday'
gen day2elec`l'`elecday' = 0
replace day2elec`l'`elecday' = day-`elecday'
replace day2elec`l'`elecday' = . if ccode != `l'

}
}


levelsof ccode, local(levels)

foreach l of local levels {
di `l'
egen day2elecmax`l' = rowmax(day2elec`l'*)
egen day2elecmin`l' = rowmin(day2elec`l'*)
gen dist2elec`l' = .
replace dist2elec`l' = day2elecmax`l' if abs(day2elecmax`l') < abs(day2elecmin`l')
replace dist2elec`l' = day2elecmin`l' if abs(day2elecmax`l') >= abs(day2elecmin`l')

}

gen dist2elec = .
levelsof ccode, local(levels)

foreach l of local levels {
di `l'
replace dist2elec = dist2elec`l' if ccode == `l'

}

* different cutoffs
preserve

drop if dist2elec > 30
drop if dist2elec < -30
drop day2elec*
collapse (mean) numb_allevents, by(dist2elec)
count
export delimited using "dist2elec30contest.csv", replace

restore


preserve

drop if dist2elec > 180
drop if dist2elec < -180
drop day2elec*
collapse (mean) numb_allevents, by(dist2elec)
count
export delimited using "dist2elec180contest.csv", replace

restore


preserve

drop if dist2elec > 365
drop if dist2elec < -365
drop day2elec*
collapse (mean) numb_allevents, by(dist2elec)
count
export delimited using "dist2elec365contest.csv", replace

restore


*************
* NO CONTEST
*************

use "scad_nelda_master.dta", clear
desc
count

sort ccode day


* No elections
bys ccode: summ numbelec_all_NOcontest
drop if ccode == 51
drop if ccode == 52
drop if ccode == 520
drop if ccode == 570
drop if ccode == 620

* distance to election
levelsof ccode, local(levels)

foreach l of local levels {
di `l'

levelsof day if numbelec_all_NOcontest & ccode == `l', local(elecdaylist)

foreach elecday of local elecdaylist {

di `elecday'
cap drop day2elec`l'`elecday'
gen day2elec`l'`elecday' = 0
replace day2elec`l'`elecday' = day-`elecday'
replace day2elec`l'`elecday' = . if ccode != `l'

}
}


levelsof ccode, local(levels)

foreach l of local levels {
di `l'
egen day2elecmax`l' = rowmax(day2elec`l'*)
egen day2elecmin`l' = rowmin(day2elec`l'*)
gen dist2elec`l' = .
replace dist2elec`l' = day2elecmax`l' if abs(day2elecmax`l') < abs(day2elecmin`l')
replace dist2elec`l' = day2elecmin`l' if abs(day2elecmax`l') >= abs(day2elecmin`l')

}

gen dist2elec = .
levelsof ccode, local(levels)

foreach l of local levels {
di `l'
replace dist2elec = dist2elec`l' if ccode == `l'

}


* different cutoffs
preserve

drop if dist2elec > 30
drop if dist2elec < -30
drop day2elec*
collapse (mean) numb_allevents, by(dist2elec)
count
export delimited using "dist2elec30NOcontest.csv", replace


restore


preserve

drop if dist2elec > 180
drop if dist2elec < -180
drop day2elec*
collapse (mean) numb_allevents, by(dist2elec)
count
export delimited using "dist2elec180NOcontest.csv", replace

restore


preserve

drop if dist2elec > 365
drop if dist2elec < -365
drop day2elec*
collapse (mean) numb_allevents, by(dist2elec)
count
export delimited using "dist2elec365NOcontest.csv", replace

restore


****************************************
* WITH PRO-GOVT and ANTI-GOVT VIOLENCE 
****************************************

use "scad_nelda_master.dta", clear
desc
count

sort ccode day


* No elections
bys ccode: summ numbelec_all
drop if ccode == 520
drop if ccode == 620

* distance to election
levelsof ccode, local(levels)

foreach l of local levels {
di `l'

levelsof day if numbelec_all & ccode == `l', local(elecdaylist)

foreach elecday of local elecdaylist {

di `elecday'
cap drop day2elec`l'`elecday'
gen day2elec`l'`elecday' = 0
replace day2elec`l'`elecday' = day-`elecday'
replace day2elec`l'`elecday' = . if ccode != `l'

}
}


levelsof ccode, local(levels)

foreach l of local levels {
di `l'
egen day2elecmax`l' = rowmax(day2elec`l'*)
egen day2elecmin`l' = rowmin(day2elec`l'*)
gen dist2elec`l' = .
replace dist2elec`l' = day2elecmax`l' if abs(day2elecmax`l') < abs(day2elecmin`l')
replace dist2elec`l' = day2elecmin`l' if abs(day2elecmax`l') >= abs(day2elecmin`l')

}

gen dist2elec = .
levelsof ccode, local(levels)

foreach l of local levels {
di `l'
replace dist2elec = dist2elec`l' if ccode == `l'

}

* different cutoffs
preserve

drop if dist2elec > 30
drop if dist2elec < -30
drop day2elec*
collapse (mean) numb_progovt* numb_antigovt*, by(dist2elec)
count
export delimited using "dist2elec30antigovt2.csv", replace

restore

preserve

drop if dist2elec > 45
drop if dist2elec < -45
drop day2elec*
collapse (mean) numb_progovt* numb_antigovt*, by(dist2elec)
count
export delimited using "dist2elec45progovt.csv", replace

restore


**************************
* SPECIFIC COUNTRY GRAPHS
**************************

* yearly version

use "scad_nelda_year_master.dta", clear
desc
count

sort ccode year

list ccode countryname if countryname == "Kenya"
list ccode countryname if countryname == "Ghana"
list ccode countryname if countryname == "Burkina Faso"
list ccode countryname if countryname == "Nigeria"

foreach l of numlist 501 452 439 475 {

preserve

keep if ccode == `l'
export delimited using "`l'year.csv", replace

restore

}



* monthly version as requested by a referee, not used in final version

use "scad_nelda_year_month_master.dta", clear
desc
count

sort ccode year month

foreach l of numlist 501 452 439 475 {

preserve

keep if ccode == `l'
export delimited using "`l'month.csv", replace

restore

}

